<?php
/**
*
* Contains shop functions for the front-end
*
* @package	VirtueMart
* @subpackage Helpers
*
* @author RolandD
* @author Max Milbers
* @link http://www.virtuemart.net
* @copyright Copyright (c) 2004 - 2010 VirtueMart Team. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* @version $Id: shopfunctionsf.php 3355 2011-05-24 13:20:35Z Milbo $
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

class shopFunctionsF {

	/**
	 * Prepares a view for rendering email, then renders and sends
	 *
	 * @param object $controller
	 * @param string $viewName View which will render the email
	 * @param string $recipient shopper@whatever.com
	 * @param array $vars variables to assign to the view
	 */
	public static function renderMail ($viewName, $recipient, $vars=array()) {
		$format = (ORDER_MAIL_HTML) ? 'json' : 'raw';
		$format = 'json'; // here only json view available but email will be html

		//$controller = new VirtueMartControllerVirtuemart();
		// Require the base controller
		require_once (JPATH_COMPONENT.DS.'controller.php');

		// Create the controller
		$controller = MvmController::getInstance("Mvm");		
		$controller->addModelPath(JPATH_COMPONENT.DS.'models');

		$view = $controller->getView($viewName, $format);
		$model = $controller->getModel($viewName);
		if ($model) {
			$view->setModel($model);
		}
		$view->setModel($controller->getModel('user', 'VirtuemartModel'), false);
		$view->setModel($controller->getModel('vendor', 'VirtuemartModel'), false);
		$view->setModel($controller->getModel('userfields', 'VirtuemartModel'), false);

		foreach ($vars as $key => $val) {
			$view->$key = $val;
		}

		return self::sendMail($view, $recipient);

		if (isset($view->doVendor)) {
			return self::sendMail($view, $view->vendorEmail, true);
		}
	}
	// VirtueMartViewUser: registerUser,

	/**
	 * With this function you can use a view to sent it by email.
	 * Just use a task in a controller todo the rendering of the email.
	 *
	 * @param string $view for exampel user, cart
	 * @param string $recipient shopper@whatever.com
	 * @param bool $vendor true for notifying vendor of user action (e.g. registration)
	 */
	private static function sendMail (&$view, $recipient, $vendor=false) {
		ob_start();
		$view->renderMail($vendor);
		$body = ob_get_contents();
		ob_end_clean();

		$subject = (isset($view->subject)) ? $view->subject : JText::_('COM_VIRTUEMART_DEFAULT_MESSAGE_SUBJECT');
		$mailer = JFactory::getMailer();
		$mailer->addRecipient($recipient);
		$mailer->setSubject($subject);
		$mailer->isHTML(true);
		$mailer->setBody($body);

		if (isset($view->replyTo)) {
			$mailer->addReplyTo($view->replyTo);
		}

		if (isset($view->mediaToSend)) {
			foreach ((array)$view->mediaToSend as $media) {
				//Todo test and such things.
				$mailer->addAttachment($media);
			}
		}

		$mailer->Send();
		return $body;
	}


	/**
	 * Sends the mail using joomla mailer
	 * TODO people often send media with emails. Like pictures, serials,...
	 *
	 * @author Max Milbers
	 * @param $body the html body to send, the content of the email
	 * @param $recipient the recipients of the mail, can be array also
	 * @param $mediaToSend an array for the paths which holds the files which should be sent to
	 * @param $vendorId default is 1 (mainstore)
	 * @deprecated
	 */
	public function sendVmMail ($body, $recipient, $subject='', $replyTo=array(), $mediaToSend=array()) {
		$subject = (empty($subject)) ? JText::_('COM_VIRTUEMART_DEFAULT_MESSAGE_SUBJECT') : $subject;
		$mailer = JFactory::getMailer();
		$mailer->addRecipient($recipient);
		$mailer->setSubject($subject);
		$mailer->isHTML(VmConfig::get('html_email',true));
		$mailer->setBody($body);

		if (!empty($replyTo)) {
			$mailer->addReplyTo($replyTo);
		}

		// Optional file attached  //this information must come from the cart
		if (!empty($mediaToSend)) {
			foreach ((array)$mediaToSend as $media) {
				//Todo test and such things.
				$mailer->addAttachment($media);
			}
		}

		return $mailer->Send();
	}

	/**
	 * Sends the mail joomla conform
	 * TODO people often send media with emails. Like pictures, serials,...
	 *
	 * @author Max Milbers
	 * @param $body the html body to send, the content of the email
	 * @param $recipient the recipients of the mail, can be array also
	 * @param $mediaToSend an array for the paths which holds the files which should be sent to
	 * @param $vendorId default is 1 (mainstore)
	 * @deprecated
	 */
	function sendVmMailold($body,$recipient,$subject='TODO set subject', $virtuemart_vendor_id=1, $mediaToSend = false ){

		$mailer =& JFactory::getMailer();

		//This is now just without multivendor
		$config =& JFactory::getConfig();
		$sender = array(
    	$config->getValue( 'config.mailfrom' ),
    	$config->getValue( 'config.fromname' ) );

		$mailer->setSender($sender);

		$mailer->addRecipient($recipient);

		$mailer->setSubject($subject);

		// Optional file attached  //this information must come from the cart
		if($mediaToSend){
			//Test if array, if not make an array out of it
			foreach ($mediaToSend as $media){
				//Todo test and such things.
				$mailer->addAttachment($media);
			}
		}

		$mailer->isHTML(true);
		$mailer->setBody($body);

		// Optionally add embedded image  //TODO Test it
		$vendor = $this->getModel('vendor','VirtuemartModel');
		$vendor->setId($virtuemart_vendor_id);
		$_store = $vendor->getVendor();

		$mailer->AddEmbeddedImage( VmConfig::get('media_path').DS.$_store->virtuemart_media_id, 'base64', 'image/jpeg' );

		return $mailer->Send();

		//Perfect Exampel for a misplaced return message. The function is used in different locations, so the messages should be set there!
//		if ( $send !== true ) {
//		    echo 'Error sending email: ' . $send->message;
//		} else {
//		    echo 'Mail sent';
//		}

	}

}

// pure php no closing tag
